#include <CGAL/Polyhedron_incremental_builder_3.h>
#include "..\structure\Enriched_polyhedron.h"

template <class HDS>
class Builder_obj : public CGAL::Modifier_base<HDS>
{
private:
	typedef typename HDS::Vertex::Point Point;
	typedef typename CGAL::Polyhedron_incremental_builder_3<HDS> Builder;
	FILE *m_pFile;

public:
	Builder_obj(FILE *pFile)
	{
		m_pFile = pFile;
	}
	~Builder_obj() {}

	void operator()(HDS& hds)
	{
		Builder builder(hds,true);
		builder.begin_surface(3,1,6);
		read_vertices(builder);
		read_facets(builder);
		builder.end_surface();
	}

private:
  // read vertex coordinates
	void read_vertices(Builder &builder)
	{
		fseek(m_pFile, 0, SEEK_SET);
  		char pLine[512];
		while (fgets(pLine, 512, m_pFile)) {
  			if (pLine[0] == 'v') {
      			float x,y,z;
				if (sscanf(pLine,"v %f %f %f",&x,&y,&z) == 3) {
					builder.add_vertex(Point(x,y,z));
				}
  			}
		}
	}

	// read facets and uv coordinates per halfedge
	void read_facets(Builder &builder)
	{
		fseek(m_pFile, 0, SEEK_SET);
//   		ifstream str(m_pFile);
// 		string buf,buf1;
// 		int fvid1, fvid2, fvid3;
// 		if(str)
// 		{
// 			while(str>>buf, !str.eof()){
// 				if (buf == "f")
// 				{
// 					builder.begin_facet();// create facet     
// 					str>>fvid1>>buf>>fvid2>>buf1>>fvid3;
// 					builder.add_vertex_to_facet(fvid1-1);
// 					builder.add_vertex_to_facet(fvid2-1);
// 					builder.add_vertex_to_facet(fvid3-1);
// 					builder.end_facet();
// 				}					
// 			}				
// 		}
		int dash_mode = -1 ;// 0 :"/", 1 :"//"
		char pLine[512];	char comma;
		while(fgets(pLine, 512, m_pFile))
		{
			char *pTmp = pLine;
		
			if (pTmp[0] == 'f')
			{
				int index;
				char index_ascii[512], n_ascii[512];
			
				// create facet        
				builder.begin_facet();
				pTmp += 2; // jump after 'f '
				while(sscanf(pTmp, "%d%c%s",&index,&comma,n_ascii) )
				{
					_itoa(index, index_ascii, 10);
					if(comma==','){
						pTmp += (1 + strlen(index_ascii) + strlen(n_ascii));
						strcat(index_ascii,n_ascii);
						index = atoi(index_ascii);
						builder.add_vertex_to_facet(index-1);
					}
					else if(comma=='/'){
						builder.add_vertex_to_facet(index-1);
						if(n_ascii[0]=='/'){
							pTmp += (1 + strlen(index_ascii) + strlen(n_ascii));
							//dash_mode  = 1 ;
						}
						else{
							pTmp += (1 + strlen(index_ascii) + strlen(n_ascii));
							//dash_mode = 0;
						}	
					}
					else{
						builder.add_vertex_to_facet(index-1);
						pTmp += strlen(index_ascii);
					}
					
					if(strlen(pTmp) < 3){
						break;
					} else {
						pTmp += 1;
					}
				}
				builder.end_facet();
			}
		}		
	}
};

template <class	kernel,	class items>
class Parser_obj
{
public:
	typedef typename Enriched_polyhedron<kernel, items>::HalfedgeDS HalfedgeDS;
	Parser_obj() {}
	~Parser_obj() {}

public:
	bool read(const char*pFilename,Enriched_polyhedron<kernel,items> *pMesh)
	{
		CGAL_assertion(pMesh != NULL);
		FILE *pFile = fopen(pFilename,"rt");
		if (pFile == NULL)
			return false;
		Builder_obj<HalfedgeDS> builder(pFile);
		pMesh->delegate(builder);
		fclose(pFile);
		return true;
	}
};